package com.telenav;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class EdgeDetectDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private String calType = "Roberts";
	private int edgeType = 0;
	private Button btnEdgeThining;
	private Button btnGradsOnly;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public EdgeDetectDialog(Shell parent, int style) {
		super(parent, style);
		setText("Edge Detect");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			
			StringBuffer sb = new StringBuffer();
			sb.append(calType+";");
			sb.append(edgeType+";");
			sb.append(((text.getText()==null || text.getText().equals(""))?"0":text.getText())+";");
			sb.append(btnEdgeThining.getSelection()+";");
			sb.append(btnGradsOnly.getSelection());
			result = sb.toString();
			
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(306, 312);
		shell.setText(getText());
		shell.setLayout(null);
		
		Group grpEdgeDerection = new Group(shell, SWT.NONE);
		grpEdgeDerection.setText("Edge Derection");
		grpEdgeDerection.setBounds(143, 10, 137, 174);
		
		Button btnAll = new Button(grpEdgeDerection, SWT.RADIO);
		btnAll.setBounds(10, 23, 90, 16);
		btnAll.setText("All");
		btnAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edgeType = 0;
			}
		});
		
		final Button btnHorizon = new Button(grpEdgeDerection, SWT.RADIO);
		btnHorizon.setBounds(10, 53, 90, 16);
		btnHorizon.setText("Horizon");
		btnHorizon.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edgeType = 1;
			}
		});
		
		final Button btnVertical = new Button(grpEdgeDerection, SWT.RADIO);
		btnVertical.setBounds(10, 85, 90, 16);
		btnVertical.setText("Vertical");
		btnVertical.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edgeType = 2;
			}
		});
		
		Button btnDeg = new Button(grpEdgeDerection, SWT.RADIO);
		btnDeg.setBounds(10, 114, 90, 16);
		btnDeg.setText("45 Deg");
		btnDeg.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edgeType = 3;
			}
		});
		
		Button btnDeg_1 = new Button(grpEdgeDerection, SWT.RADIO);
		btnDeg_1.setBounds(10, 148, 90, 16);
		btnDeg_1.setText("135 Deg");
		btnDeg_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edgeType = 4;
			}
		});
		
		Group grpCalcType = new Group(shell, SWT.NONE);
		grpCalcType.setText("Calc Type");
		grpCalcType.setBounds(10, 10, 103, 174);
		
		Button btnRoberts = new Button(grpCalcType, SWT.RADIO);
		btnRoberts.setBounds(10, 23, 90, 16);
		btnRoberts.setText("Roberts");
		btnRoberts.setSelection(true);
		btnRoberts.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				calType = "Roberts";
				btnHorizon.setEnabled(false);
				btnVertical.setEnabled(false);
			}
		});		
		
		Button btnSobel = new Button(grpCalcType, SWT.RADIO);
		btnSobel.setBounds(10, 52, 90, 16);
		btnSobel.setText("Sobel");
		btnSobel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				calType = "Sobel";
				btnHorizon.setEnabled(true);
				btnVertical.setEnabled(true);
			}
		});		
		
		Button btnPrevitt = new Button(grpCalcType, SWT.RADIO);
		btnPrevitt.setBounds(10, 85, 90, 16);
		btnPrevitt.setText("Previtt");
		btnPrevitt.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				calType = "Previtt";
				btnHorizon.setEnabled(true);
				btnVertical.setEnabled(true);
			}
		});		
		
		Button btnLog = new Button(grpCalcType, SWT.RADIO);
		btnLog.setBounds(10, 114, 90, 16);
		btnLog.setText("LoG");
		btnLog.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				calType = "LoG";
			}
		});		
		
		Button btnCanny = new Button(grpCalcType, SWT.RADIO);
		btnCanny.setBounds(10, 148, 90, 16);
		btnCanny.setText("Canny");
		btnCanny.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				calType = "Canny";
			}
		});		
		
		Label lblThreshold = new Label(shell, SWT.NONE);
		lblThreshold.setBounds(10, 204, 55, 15);
		lblThreshold.setText("ThresHold");
		
		text = new Text(shell, SWT.BORDER);
		text.setText("0");
		text.setBounds(88, 204, 73, 21);
		
		btnEdgeThining = new Button(shell, SWT.CHECK);
		btnEdgeThining.setBounds(10, 240, 93, 16);
		btnEdgeThining.setText("Edge Thining");
		btnEdgeThining.setSelection(true);
		
		btnGradsOnly = new Button(shell, SWT.CHECK);
		btnGradsOnly.setBounds(143, 240, 93, 16);
		btnGradsOnly.setText("Grads Only");
		

	}
}
